package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.Banner;
import com.example.domain.model.BannerDTO;
import com.example.domain.model.MyPage;
import com.example.service.BannerService;
import com.example.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Api(tags = "banner图管理")
@RestController
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    @Value("${upload.bannerPath}")
    private String uplodaPath;

    @ApiOperation(value = "banner图列表")
    @PostMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页索引", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示记录数", defaultValue = "20")
    })
    public R<IPage<BannerDTO>> getList(@RequestBody MyPage myPage){
        Page<Banner> page = new Page(myPage.getCurrent(),myPage.getSize());
        return R.success(bannerService.getList(page));
    }

    @ApiOperation(value = "查看")
    @GetMapping("/{id}")
    public R<Banner> getInfo(@PathVariable String id){
        return R.success(bannerService.getById(id));
    }

    @ApiOperation(value = "新增banner图")
    @PostMapping
    public R add(@RequestBody Banner banner){
        return bannerService.save(banner) ? R.success() : R.error();
    }

    @ApiOperation(value = "上传banner图")
    @PostMapping("/upload")
    public R uploda(@RequestParam("bannerFile") MultipartFile multipartFile){
        String bannerURL = FileUtil.saveBanner(multipartFile, uplodaPath);
        return R.success(bannerURL);
    }

    @ApiOperation(value = "更新banner图")
    @PutMapping("/{id}")
    public R update(@PathVariable String id,@RequestBody Banner banner){
        return bannerService.update(id,banner) ? R.success() : R.error();
    }

    @ApiOperation(value = "删除banner图")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable String id){
        return bannerService.delete(id) ? R.success() : R.error();
    }
}
